﻿using Sirenix.OdinInspector;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using App.Common;
using QxFramework.Core;
using System;
using DG.Tweening;
public class CameraManager : LogicModuleBase,ICameraManager
{
    [HideInInspector]
    public Vector3 PivotPos;
    [HideInInspector]
    public Mono PlayerMono;
    private List<string> MonoName = new List<string>();
    private GameObject MainCam;
    private float cameraSize;
    public override void Init()
    {
        base.Init();
        PlayerMono = new Mono();
        MainCam = null;

        cameraSize = 8;

    }
    public override void FixedUpdate()
    {
        base.FixedUpdate();
    }
    public void UpdateCamera()
    {
        if (GameObject.Find("Main Camera") == null)
        {
            return;
        }
        if (MainCam == null)
        {
            MainCam = GameObject.Find("Main Camera");
        }
        List<string> nameList = new List<string>();
        for(int i = 0;i< MonoName.Count;i++)
        {
            if (GameMgr.Get<IMonoManager>().GetAllMono()._MonoList.ContainsKey(MonoName[i]))
                nameList.Add(MonoName[i]);
        }
        if (GameMgr.Get<IMonoManager>().GetAllMono()._MonoList.Count != 0)
        {
            int count = nameList.Count;
            if (count == 0)
                return;
            Vector3 pos = new Vector3();
            Vector3 drc = new Vector3();
            float preX = GameMgr.Get<IMonoManager>().GetAllMono()._MonoList[nameList[0]].Position.x;
            float preY = GameMgr.Get<IMonoManager>().GetAllMono()._MonoList[nameList[0]].Position.y;
            for (int i = 0;i < count ;i++)
            {
                
                pos += GameMgr.Get<IMonoManager>().GetAllMono()._MonoList[nameList[i]].Position;
                drc += GameMgr.Get<IMonoManager>().GetAllMono()._MonoList[nameList[i]].Direction;
            }
            pos /= count;
            drc /= count;
            float distance = Mathf.Sqrt((preX - pos.x) * (preX - pos.x) + (preY - pos.y) * (preY - pos.y));
            Vector3 Pos = pos + drc / 6;
            PivotPos = new Vector3(Pos.x, Pos.y, MainCam.transform.position.z);
            if (GetPosDistance(PivotPos, MainCam.transform.position) > 1000.0)
                MainCam.transform.position = PivotPos;
            MainCam.transform.position = new Vector3(
                Mathf.Lerp(MainCam.transform.position.x, Pos.x, 0.25f / (PivotPos - MainCam.transform.position).magnitude),
                Mathf.Lerp(MainCam.transform.position.y, Pos.y, 0.25f / (PivotPos - MainCam.transform.position).magnitude),
                MainCam.transform.position.z);
            MainCam.GetComponent<Camera>().orthographicSize = cameraSize + Mathf.Log10(distance + 1);
        }
    }
    private double GetPosDistance(Vector3 pos1,Vector3 pos2)
    {
        return (pos1.x - pos2.x) * (pos1.x - pos2.x) + (pos1.y - pos2.y) * (pos1.y - pos2.y);
    }
    public void SetPlayerMono(string Name)
    {
        if(!MonoName.Contains(Name))
            MonoName.Add(Name);
    }
    public void SetBatlleMono(string Name)
    {
        MonoName.Clear();
        MonoName.Add(Name);
    }
}
